#!user/bin/env python3
# -- coding: utf-8 --
# @Createtime：2022/5/15
# @Updatetime：
# @Author：Test008
# @File：DeleteStuInfo.py
# @Description：删除学生信息
import os.path
from config import filename


def delete():
    while True:
        student_id = input('请输入学生ID，如1001：')
        if os.path.exists(filename):
            with open(filename, 'r', encoding='utf-8') as rfile:
                # 如果文件存在，读取文件内的信息
                student_old = rfile.readlines()
                # print(student_old, type(student_old))
        else:
            student_old = []  # 文件不存在，返回空列表
        flag = False# 标记是否删除
        if student_old:
            with open(filename, 'w', encoding='utf8') as wfile:
                d = {}
                for i in student_old:
                    # print(type(str), str)
                    # 字符转回字典
                    d = dict(eval(i))
                    if d['id'] != student_id:
                        wfile.write(str(d) + '\n')
                    else:
                        flag = True
                if flag:
                    answer = input(f'确定要删除ID为{student_id}学生信息吗，y/n')
                    if answer == 'y' or answer == 'Y':
                        print(f'ID为{student_id}的学生已被删除。')
                    else:
                        break
                else:
                    print(f'没有找到ID为{student_id}的学生信息')
        else:
            print('无学生信息')
            break
            show()  # 删除之后要重新显示所有学生信息
        answer = input('是否继续选择删除，y/n')
        if answer == 'y' or answer == 'Y':
            continue
        else:
            break


if __name__ == '__main__':
    delete()
